package com.hjms.common.entity.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author 何杨谊
 * @description: 全局异常处理
 * @date 2023/2/8 14:42
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    /**
     * 401=未授权
     * @param e
     * @return
     */
    @ExceptionHandler(Unauthorized401Exception.class)
    public ResponseEntity handleUnauthorized401Exception(Unauthorized401Exception e) {
        return ResponseEntity
                .status(HttpStatus.PRECONDITION_FAILED)
                .body(ErrorResult.Unauthorized401(e.getMessage(),e.getCode()));
    }

    /**
     * 403=未授权
     * @param e
     * @return
     */
    @ExceptionHandler(Forbidden403Exception.class)
    public ResponseEntity handleForbidden403Exception(Forbidden403Exception e) {
        return ResponseEntity
                .status(HttpStatus.PRECONDITION_FAILED)
                .body(ErrorResult.Forbidden403(e.getMessage(),e.getCode()));
    }

    /**
     * 412=先决条件不满足
     * @param e
     * @return
     */
    @ExceptionHandler(Precondition412Exception.class)
    public ResponseEntity handlePrecondition412Exception(Precondition412Exception e) {
        return ResponseEntity
                .status(HttpStatus.PRECONDITION_FAILED)
                .body(ErrorResult.Precondition412(e.getMessage(),e.getCode()));
    }

    /**
     * 500=服务异常
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public ResponseEntity handlerAllException(Exception e) {
        return ResponseEntity
                .status(HttpStatus.INTERNAL_SERVER_ERROR)
                .body(ErrorResult.ServerError500(e.getMessage()));
    }
}
